﻿using System;
using System.Web.Security;
using System.Web.UI.WebControls;
using FatFreeFitness._models;

namespace FatFreeFitness.membership
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
        {
            var ctlStepContainer = Step0.ContentTemplateContainer;
            CreateUserWizard1.Email = ((TextBox)ctlStepContainer.FindControl("Username")).Text;
        }

        protected void CreateUserWizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (CreateUserWizard1.ActiveStep == Step1)
            {
                var ctlStepContainer = Step1.ContentTemplateContainer;
                var firstName = ((TextBox) ctlStepContainer.FindControl("txtFirstName")).Text;
                var lastName = ((TextBox) ctlStepContainer.FindControl("txtLastName")).Text;
                var gender = ((DropDownList) ctlStepContainer.FindControl("ddlGender")).SelectedValue;
                var birthDate = DateTime.Parse(((TextBox) ctlStepContainer.FindControl("txtBirthDate")).Text);

                var entities = new FatFreeFitnessEntities();
                var member = new Member()
                                 {
                                     MembershipKey = (Guid) Membership.GetUser(CreateUserWizard1.Email).ProviderUserKey,
                                     FirstName = firstName,
                                     LastName = lastName,
                                     Gender = gender,
                                     BirthDate = birthDate,
                                     RoutineID = 1,
                                 };
                entities.AddToMembers(member);
                entities.SaveChanges();
            }
        }
    }
}